package com.jf.StudenManage.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jf.StudenManage.pojo.Student;
import com.jf.StudenManage.service.StudentService;

public class StudentServlet extends HttpServlet {
   
	private StudentService studentService = new StudentService();
	 
    @Override
    public void init() throws ServletException {
         this.getServletContext().setAttribute("total",0);
    }
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();
		if ("/register.do".equals(servletPath)) {
            Student student=new Student();
            student.setGender(request.getParameter("gender"));
			student.setName(request.getParameter("username"));
			student.setPassword(request.getParameter("password"));
			student.setSno(request.getParameter("sno"));
			student.setTelephone(request.getParameter("telephone"));
			studentService.register(student);
			response.sendRedirect("login.jsp");
			
		} else if ("/login.do".equals(servletPath)) {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=utf-8");

			String content = request.getParameter("username");
			String password = request.getParameter("password");
			String code = request.getParameter("code");
			// 校验 用户填写的验证码 是否正确如果不正确我给他踢回到 登陆页面
			HttpSession session = request.getSession();
			String answer = (String) session.getAttribute("answer");
			if (answer.equals(code)) {// 证明验证码是正确的才能走接下来登陆的业务逻辑
				String result = studentService.login(content, password,
						response);
//				System.out.println(result);
				int  total=(int) this.getServletContext().getAttribute("total");
				total=total+1;
				this.getServletContext().setAttribute("total",total);
				Object[]  obj=studentService.backAllStudentsByPaginer("1");
				
				request.setAttribute("pageCount", obj[0]);
				request.setAttribute("studentlist",obj[1]);
				request.getRequestDispatcher("list.jsp").forward(request, response);// 期间发生了两次请求与响应
			} else {
				response.sendRedirect("login.jsp");
			}
		}else  if("/update.do".equals(servletPath)){
			Student  student=new  Student();
			student.setGender(request.getParameter("gender"));
			student.setName(request.getParameter("name"));
			student.setPassword(request.getParameter("password"));
			student.setSid(Integer.parseInt(request.getParameter("sid")));
			student.setSno(request.getParameter("sno"));
			student.setTelephone(request.getParameter("telephone"));
			request.setAttribute("studentlist",studentService.updateStudentById(student) );
			request.getRequestDispatcher("list.jsp").forward(request, response);
			
		}

		/*
		 * response.setHeader("flag", "1000"); response.addHeader("flag1",
		 * "644365"); PrintWriter out= response.getWriter(); out.write(result);
		 * out.flush(); out.close();
		 */
		// OutputStream out=response.getOutputStream();
		// out.write(result.getBytes("UTF-8"));
		// out.flush();
		// out.close();

	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	   
		String servletPath = request.getServletPath();
		
		System.out.println(servletPath);
		if("/delete.do".equals(servletPath)){
			String  sid=	request.getParameter("sid");
			request.setAttribute("studentlist", studentService.deleStudentById(sid));
			request.getRequestDispatcher("list.jsp").forward(request, response);
         }else  if("/update.do".equals(servletPath)){
        	 String  sid=request.getParameter("sid");
        	 request.setAttribute("student", studentService.backStudentById(sid));
        	 request.getRequestDispatcher("update.jsp").forward(request, response);
         }else  if("/deleteSome.do".equals(servletPath)){
        	 String  sids=request.getParameter("sids");
        	 request.setAttribute("studentlist",studentService.deleteSomeByIds(sids));
        	 request.getRequestDispatcher("list.jsp").forward(request, response);
         }else  if("/pagion.do".equals(servletPath)){
        	 String  pageNum=request.getParameter("pageNum");
        	 Object[]  obj=studentService.backAllStudentsByPaginer(pageNum);
        	 request.setAttribute("pageCount", obj[0]);
        	 request.setAttribute("studentlist",obj[1]);
        	 request.getRequestDispatcher("list.jsp").forward(request, response);
         }
		
		
		
	   
	}

}
